ActivationGrad
激活函数梯度计算算子系列。该系列算子根据激活函数的输入(或输出)以及上一层传回的梯度,计算并输出当前层的梯度。
其中 \(src0\) 为梯度输入(Output Gradient),\(src1\) 为激活函数的原始输入或输出(取决于具体激活函数类型),\(f'\) 为激活函数的导数。
- 包含算子列表:
ReluGrad: ReLU 激活梯度。
Relu6Grad: ReLU6 激活梯度。
LeakyReluGrad (l_relu_grad): Leaky ReLU 激活梯度,需传入参数
alpha。SigmoidGrad: Sigmoid 激活梯度。
TanhGrad: Tanh 激活梯度。
HSigmoidGrad: Hard Sigmoid 激活梯度。
HSwishGrad: Hard Swish 激活梯度。
GeluGrad: GELU 激活梯度。
EluGrad: ELU 激活梯度,需传入参数
alpha。SoftplusGrad: Softplus 激活梯度。
HardShrinkGrad: Hard Shrink 激活梯度,需传入参数
lambd。SoftshrinkGrad: Softshrink 激活梯度,需传入参数
lambd。
- 输入:
src0 - 梯度输入数据地址。
src1 - 原始输入/输出数据地址。
length - 计算长度。
alpha / lambd (float, 可选) - 特定激活函数所需的系数。
core_mask (int, 可选) - 核掩码(仅适用于共享存储版本)。
- 输出:
dst - 梯度计算结果输出地址。
- 支持平台:
FT78NEMT7004
备注
FT78NE 仅支持 fp32。
MT7004 支持 fp32, fp16。
对于不同的算子,
src1的含义可能不同(例如 SigmoidGrad 通常使用前向传播的输出作为 src1,而 ReluGrad 使用前向传播的输入作为 src1),需确保上层传入地址正确。
共享存储版本:
-
void fp_relu_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void hp_relu_grad_s(half *src0, half *src1, half *dst, int length, int core_mask)
-
void fp_relu6_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_l_relu_grad_s(float *src0, float *src1, float *dst, int length, float alpha, int core_mask)
-
void fp_sigmoid_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_tanh_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_h_sigmoid_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_h_swish_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_gelu_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_elu_grad_s(float *src0, float *src1, float *dst, int length, float alpha, int core_mask)
-
void fp_softplus_grad_s(float *src0, float *src1, float *dst, int length, int core_mask)
-
void fp_hard_shrink_grad_s(float *src0, float *src1, float *dst, int length, float lambd, int core_mask)
-
void fp_softshrink_grad_s(float *src0, float *src1, float *dst, int length, float lambd, int core_mask)
C调用示例:
1//FT78NE示例(共享存储) 2#include <stdio.h> 3#include "78NE/utils.h" 4 5int main(int argc, char* argv[]) { 6 float *src0 = (float *)0xA0000000; // 梯度输入在共享存储 7 float *src1 = (float *)0xA1000000; // 原始数据在共享存储 8 float *dst = (float *)0xB0000000; // 结果输出到共享存储 9 int length = 1024; 10 int core_mask = 0xff; 11 fp_relu_grad_s(src0, src1, dst, length, core_mask); 12 return 0; 13}
私有存储版本:
-
void fp_relu_grad_p(float *src0, float *src1, float *dst, int length)
-
void hp_relu_grad_p(half *src0, half *src1, half *dst, int length)
-
void fp_relu6_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_l_relu_grad_p(float *src0, float *src1, float *dst, int length, float alpha)
-
void fp_sigmoid_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_tanh_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_h_sigmoid_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_h_swish_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_gelu_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_elu_grad_p(float *src0, float *src1, float *dst, int length, float alpha)
-
void fp_softplus_grad_p(float *src0, float *src1, float *dst, int length)
-
void fp_hard_shrink_grad_p(float *src0, float *src1, float *dst, int length, float lambd)
-
void fp_softshrink_grad_p(float *src0, float *src1, float *dst, int length, float lambd)
C调用示例:
1//MT7004 示例 2#include <stdio.h> 3 4int main(int argc, char* argv[]) { 5 float *src0 = (float *)0x10000000; // 私有存储空间地址 6 float *src1 = (float *)0x10001000; 7 float *dst = (float *)0x10002000; 8 int length = 139; 9 float alpha = 0.01f; 10 fp_l_relu_grad_p(src0, src1, dst, length, alpha); 11 return 0; 12}